<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
	$return['status']=10;
	if($_RQ['ismyitem']){
		$this->session('ismyitem',intval($_RQ['ismyitem']));
	}
	$return['ismyitem']=$this->session('ismyitem');
    $params=array('uniacid'=>$_SESSION['uniacid'],'type'=>1);
    if($return['ismyitem']==1){
        $pids = pdo_getall('jy_quan_item',array('mid'=>$_SESSION['uid']),array('id'),'id');
        if($pids){
            $pids=array_keys($pids);
            $params['pid']=$pids;
        }else{
            $params['pid']=-1;
        }
    }else{
        $params['mid']=$_SESSION['uid'];
    }
    $list=pdo_getall('jy_quan_item_jieli',$params,array('id','orderid','pid'),'','createtime desc');
    $allgoods=array();
    $alltotal=0;
    foreach($list as &$lv){
        if($lv['orderid']){
            $formdata=pdo_get('jy_quan_item_formdata',array('orderid'=>$lv['orderid'],'uniacid'=>$_SESSION['uniacid']));
        }
        if(!$formdata){
            $formdata=pdo_get('jy_quan_item_formdata',array('j_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
        }
        $fileds=json_decode($formdata['fileds'],true);
        $address=json_decode($formdata['data'],true);
        foreach ($fileds as $k=>$fv){
            $alltotal +=$fv['total'];
            $optionid=$fv['optionid']?:0;
            $goodskey=$lv['pid'].'_'.$k.'_'.$optionid;
            if(!$allgoods[$goodskey]){
                $allgoods[$goodskey]=array(
                    'key'=>$goodskey,
                    'title'=>$fv['title'],
                    'image'=>$fv['image'],
                    'unit'=>$fv['unit'],
                    'optiontitle'=>$fv['image']?:$fv['options'][$optionid]['title'],
                    'price'=>$fv['price'],
                    'optionid'=>$fv['optionid'],
                    'total'=>$fv['total'],
                );
            }else{
                $allgoods[$goodskey]['total'] +=$fv['total'];
            }
        }
    }
    $return['params']=array('type'=>1);
    $return['allgoods']=array_values($allgoods);
    $return['alltotal']=$alltotal;
	$return['express']=Array (
		'0' => Array ('id'=> 1, 'name' => '顺丰快递' ),
		'1' => Array ( 'id' => 2, 'name' => '申通快递' ) ,
		'2' => Array ( 'id' => 3,'name' => '韵达快运' ),
		'3' => Array ('id' => 4, 'name' => '天天快递' ),
		'4' => Array ( 'id' => 5, 'name' => '圆通速递' ) ,
		'5' => Array ( 'id' => 6,'name' => '中通速递' ) ,
		'6' => Array ( 'id' => 7 ,'name' => 'ems快递' ),
		'7' => Array ( 'id' => 8, 'name' => '汇通快运' ),
		'8' => Array ( 'id' => 9, 'name' => '其它快递' )
	);
	$return['post']=array('type'=>0);
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ismyitem=$this->session('ismyitem');
	if($ismyitem==1){
		$pids = pdo_getall('jy_quan_item',array('mid'=>$_SESSION['uid']),array('id'),'id');
		if($pids){
			$pids=array_keys($pids);
			$params['pid']=$pids;
		}else{
            $params['pid']=-1;
        }
	}else{
		$params['mid']=$_SESSION['uid'];
	}
	if(isset($_RQ['status']) && intval($_RQ['status'])<10){
		if(intval($_RQ['status'])==4){
			$params['status']=array(4,5);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['status']=$_RQ['status'];
	}elseif($_RQ['status']=='10'){
		$return['status']=10;
	}else{
		$return['status']=1;
		$params['status']=1;
	}
    if(!$_RQ['type']){
        $params['type <>']=1;
    }else{
        $params['type']=$_RQ['type'];
    }
    if($_RQ['select']){
        $goodskey=str_array($_RQ['select']);
        $params['goodskey']=$goodskey;
    }
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_quan_feelog',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
			foreach($list as &$item){
				$user=cfc('core')->member_show($item['mid']);
				$info=pdo_get('jy_quan_item',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']));
				$item['user']=$user;
				$item['info']=$info;
				$item['orderid']=$item['id'];
				$item['typestatus']=$this->fee_status[$item['status']];
				$item['typetitle']=$this->itemtypes()[$info['type']];
				$item['createtime']=date('Y-m-d', $item['createtime']);
				if($_SESSION['wxapp_id']){
					//小程序二维码：
					$url='/jy_quan/verify/detail/index?id='.$item['id'].'&verifycode='.$item['verifycode'];
					$pid='verifyid'.$item['id'].'c'.$item['verifycode'];
					$item['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
				}
                $formdata=pdo_get('jy_quan_item_formdata',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
                $fileds=json_decode($formdata['fileds'],true);
                $item['goods']=array();
                foreach ($fileds as $k=>$fv){
                    $optionid=$fv['optionid']?:0;
                    $goodskey=$item['pid'].'_'.$k.'_'.$optionid;
                    if(!$item['goods'][$goodskey]){
                        $item['goods'][$goodskey]=array(
                            'title'=>$fv['title'],
                            'image'=>$fv['image'],
                            'unit'=>$fv['unit'],
                            'optiontitle'=>$fv['image']?:$fv['options'][$optionid]['title'],
                            'price'=>$fv['price'],
                            'optionid'=>$fv['optionid'],
                            'total'=>$fv['total'],
                        );
                    }else{
                        $item['goods'][$goodskey]['total'] +=$fv['total'];
                    }
                }
                if(!$item['goodskey'] && $goodskey){
                    pdo_update('jy_quan_feelog',array('goodskey'=>$goodskey),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
                }
                $address=json_decode($formdata['data'],true);
                $item['address']=array();
                $item['address']['name']=$address[0]['data'];
                foreach ($address as $av){
                    if($av['type']==8){
                        $item['address']['mobile']=$av['data'];
                    }
                    if($av['type']==7){
                        $item['address']['pcd']=$av['data'];
                        $item['address']['address']=$av['data']['xiangxi'];
                    }
                    if($av['type']==6){
                        $item['address']['remark']=$av['data'];
                    }
                }
			}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='jielilist'){
	//查询进入该页面的客户所有订单
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ismyitem=$this->session('ismyitem');
	if($ismyitem==1){
		$pids = pdo_getall('jy_quan_item',array('mid'=>$_SESSION['uid']),array('id'),'id');
		if($pids){
			$pids=array_keys($pids);
			$params['pid']=$pids;
		}else{
            $params['pid']=-1;
        }
	}else{
		$params['mid']=$_SESSION['uid'];
	}
	if(isset($_RQ['status']) && intval($_RQ['status'])<10){
		if(intval($_RQ['status'])==4){
			$params['status']=array(4,5);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=10;
	}
	if(!$_RQ['type']){
        $params['type <>']=1;
    }else{
        $params['type']=$_RQ['type'];
    }
    if($_RQ['select']){
       $goodskey=str_array($_RQ['select']);
        $params['goodskey']=$goodskey;
    }
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_quan_item_jieli',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
			foreach($list as &$item){
				$user=cfc('core')->member_show($item['mid']);
				$info=pdo_get('jy_quan_item',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']));
				$item['user']=$user;
				$item['info']=$info;
				$order=pdo_get('jy_quan_feelog',array('id'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']));
				$item['fee']=$order['fee'];
				$item['orderstatus']=$order['status'];
				$item['typestatus']=$this->jili_status[$item['status']];				
				$item['typetitle']=$this->itemtypes()[$info['type']];
				$item['createtime']=date('Y-m-d', $item['createtime']);
				$item['verifyurl']=custom_url('jy_quan/verify.detail//id='.$item['id'].'&verifycode='.$item['verifycode'],true);
				if($_SESSION['wxapp_id']){
					//小程序二维码：
					$url='/jy_quan/verify/detail/index?id='.$item['id'].'&verifycode='.$item['verifycode'];
					$pid='verifyid'.$item['id'].'c'.$item['verifycode'];
					$item['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
				}
				if($item['orderid']){
                    $formdata=pdo_get('jy_quan_item_formdata',array('orderid'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']));
                }
                if(!$formdata){
                    $formdata=pdo_get('jy_quan_item_formdata',array('j_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
                }
                $fileds=json_decode($formdata['fileds'],true);
                $item['goods']=array();
                foreach ($fileds as $k=>$fv){
                    $optionid=$fv['optionid']?:0;
                    $goodskey=$item['pid'].'_'.$k.'_'.$optionid;
                    if(!$item['goods'][$goodskey]){
                        $item['goods'][$goodskey]=array(
                            'key'=>$goodskey,
                            'title'=>$fv['title'],
                            'image'=>$fv['image'],
                            'unit'=>$fv['unit'],
                            'optiontitle'=>$fv['image']?:$fv['options'][$optionid]['title'],
                            'price'=>$fv['price'],
                            'optionid'=>$fv['optionid'],
                            'total'=>$fv['total'],
                        );
                    }else{
                        $item['goods'][$goodskey]['total'] +=$fv['total'];
                    }
                }
                if(!$item['goodskey'] && $goodskey){
                    pdo_update('jy_quan_item_jieli',array('goodskey'=>$goodskey),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
                }
                $address=json_decode($formdata['data'],true);
                $item['address']=array();
                $item['address']['name']=$address[0]['data'];
                foreach ($address as $av){
                    if($av['type']==8){
                        $item['address']['mobile']=$av['data'];
                    }
                    if($av['type']==7){
                        $item['address']['pcd']=$av['data'];
                        $item['address']['address']=$av['data']['xiangxi'];
                    }
                    if($av['type']==6){
                        $item['address']['remark']=$av['data'];
                    }
                }
			}
	}
	$return['list']=$list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']);
	if($_RQ['isfee']){
		$order=pdo_get('jy_quan_feelog',$params);
		$order['orderid']=$_RQ['id'];
	}else{
		$order=pdo_get('jy_quan_item_jieli',$params);
	}
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']));	
	//自定义字段数据
	$order['groupsendsettitle']=$item['groupsendset']==1?'快递发货':$item['groupsendset']==0?'无需物流':'自提点提货';
	$formparams=array('mid'=>$order['mid'],'pid'=>$order['pid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['isfee']){
		$formparams['orderid']=$_RQ['id'];
	}else{
		$formparams['j_id']=$_RQ['id'];
	}
	$formdata=pdo_get('jy_quan_item_formdata',$formparams);
	$order['data']=json_decode($formdata['data'],true);
	foreach($order['data'] as &$dv){
		if($dv['type']==5 && $dv['data']){
			$dv['data'] = explode(";", $dv['data']);
			foreach($dv['data'] as $k=>&$i){
				if(empty($dv['data'][$k])){
					unset($dv['data'][$k]);
				}
			}
		}
	}
	$order['diydata']=json_decode($formdata['diydata'],true);
	foreach($order['diydata'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
	}
	$order['fileds']=json_decode($formdata['fileds'],true);
	foreach($order['fileds'] as &$fv){
		foreach($fv as $fk=>&$f){
			if($f['type']==5 && $fv['data']){
				$f['data'] = explode(";", $f['data']);
				foreach($f['data'] as $k=>&$i){
					if(empty($f['data'][$k])){
						unset($f['data'][$k]);
					}
				}
			}
			if($f['type']==18){
					$fv['total'] = $post['type']!=1?1:$fv['total'];
					if($fv['options']){
						$post['fee'] +=$fv['options'][$fv['optionid']]['price']*$fv['total'];
						$f['data']=$fv['options'][$fv['optionid']]['price'];
					}else{
						$post['fee'] +=$f['data']*$fv['total'];
					}
			}
		}
		if($fv['options']){
			$fv[]=array('title'=>'所选规格','type'=>'option','data'=>$fv['options'][$fv['optionid']]['title']);
		}
	}
	$item['typestatus']=$this->jili_status[$item['status']];
	$order['typestatus']=$this->jili_status[$order['status']];
	if($order['status']>=2){
				$order['express']=pdo_get('jy_quan_order_express',array('order_id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
				$order['express']['express_name']=$return['express'][$order['express']['express_id']-1]['name'];
	}
	$order['typetitle']=$this->itemtypes()[$order['type']];
	$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
	$order['sendtime']=date('Y-m-d H:i:s', $order['sendtime']);
	$order['paytime']=date('Y-m-d H:i:s', $order['paytime']);
	$return['order']=$order;
	$return['member']=$member;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	if($_RQ['isfee']){
		pdo_update('jy_quan_feelog',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['isfee']));
		pdo_update('jy_quan_item_jieli',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$_RQ['isfee']));
        $feelog= pdo_get('jy_quan_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['isfee']));
        $this->model('commission')->order_complete($feelog);
	}else{
		pdo_update('jy_quan_item_jieli',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}
	exi('确认凭证成功');
}
if($_RQ['op']=='tuikuan'){
	if(!$_RQ['id']){
		exi('接龙不存在，请重新选择订单','error');
	}
	$postdata=get_postdata();
	if(!$postdata['remark']){
		exi('请填写退款原因！','error');
	}
	$jili=pdo_get('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($jili['orderid']){
		pdo_update('jy_quan_feelog',array('status'=>'4','tuidesc'=>$postdata['remark']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$jili['orderid']));
		pdo_update('jy_quan_item_jieli',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('退款失败！没有费用支付记录！');
	}
	exi('退款申请成功，待审核！');
}
if($_RQ['op']=='chetuikuan'){
	if(!$_RQ['id']){
		exi('接龙不存在，请重新选择订单','error');
	}
	$jili=pdo_get('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($jili['orderid']){
		pdo_update('jy_quan_feelog',array('status'=>'6','tuidesc'=>$postdata['remark']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$jili['orderid']));
		pdo_update('jy_quan_item_jieli',array('status'=>'6'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('同意退款成功！没有费用支付记录！');
	}
	exi('同意退款成功！');
}
if($_RQ['op']=='deltuikuan'){
	if(!$_RQ['id']){
		exi('接龙不存在，请重新选择订单','error');
	}
	$jili=pdo_get('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($jili['orderid']){
		pdo_update('jy_quan_feelog',array('status'=>'1','tuidesc'=>$postdata['remark']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$jili['orderid']));
		pdo_update('jy_quan_item_jieli',array('status'=>'1'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('取消退款失败！没有费用支付记录！');
	}
	exi('取消退款成功！');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_quan_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除凭证成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_quan_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi($order['pay_id']);
}